#include <stdio.h>
#include <stdlib.h>

typedef struct NODE {
    int data;
    struct NODE *next;
    struct NODE *prev;
} node;

typedef node *nodeptr;

nodeptr createList(int n) {
    nodeptr head = NULL, tail = NULL;
    for (int i = 1; i <= n; i++) {
        nodeptr item = (nodeptr)malloc(sizeof(node));
        item->data = i;
        if (head == NULL)
            head = item;
        else
            tail->next = item, item->prev = tail;
        tail = item;
    }
    tail->next = NULL;
    head->prev = NULL;
    return head;
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    nodeptr head = createList(n);
    nodeptr ptr[100010], it = head;
    for (int i = 1; i <= n; i++, it = it->next)
        ptr[i] = it;

    while (m--) {
        int mode, x, y;
        scanf("%d%d%d", &mode, &x, &y);

        nodeptr px = NULL, nx = head, py = NULL, ny = head;
        nx = ptr[x];
        px = nx->prev;
        ny = ptr[y];
        py = ny->prev;
        if (mode == 1 || mode == 2) {
            if (mode == 1 && py == nx)
                continue;
            if (mode == 2 && px == ny)
                continue;
            // del x
            if (px)
                px->next = nx->next;
            else
                head = nx->next;

            if (nx->next) {
                nx->next->prev = px;
            }

            // insert x
            if (mode == 1) {
                if (py)
                    py->next = nx;
                else
                    head = nx;
                ny->prev = nx;
                nx->prev = py;
                nx->next = ny;

            } else if (mode == 2) {
                if (ny->next)
                    ny->next->prev = nx;
                nx->prev = ny;
                nx->next = ny->next;
                ny->next = nx;
            }
        } else if (mode == 3) {
            int t = nx->data;
            nx->data = ny->data;
            ny->data = t;
            ptr[x] = ny;
            ptr[y] = nx;
        }
    }
    it = head;
    long long ans = 0;
    int tag = 1;
    while (it) {
        ans += it->data * tag;
        tag = !tag;
        it = it->next;
    }
    printf("%lld\n", ans);
}